package com.physics.mps.common.config;


import com.physics.mps.common.config.auth.CustomInterceptor;
import com.physics.mps.common.config.properties.TokenProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author mcsoft
 * @date 2023/4/4 15:14
 */
@EnableWebMvc
@RequiredArgsConstructor
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    private final TokenProperties tokenProperties;

    private final CustomInterceptor customInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (tokenProperties.isNoCheck()) {
            return;
        }
        registry.addInterceptor(customInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/**/*.html", "/**/*.css", "/**/*.js")
                .excludePathPatterns("/**/favicon.ico", "/**/error")
                .excludePathPatterns("/**/actuator/**")
                // 排除自定义
                .excludePathPatterns("/**/system/**", "/**/common/**", "/**/downloadFolder/**")
                .excludePathPatterns("/**/doc.html", "/**/webjars/**", "/**/swagger-resources", "/**/swagger")
                .excludePathPatterns("/**/export/**")
                .excludePathPatterns("/**/temp/**")
                .excludePathPatterns("/taskComputeResult/downloadOdb/**")
        ;
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }


}
